package ink.onei.production.service;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
import java.util.Properties;

/**
 * @Author: nekotako
 * @Description: TODO
 * @Date: 28/12/2023 14:39 Thursday
 */

public class SystemInfo {
    /**
     * 系统名
     */
    public static String os_name;
    /**
     * 系统架构
     */
    public static String os_arch;
    /**
     * 系统版本号
     */
    public static String os_version;
    /**
     * 系统IP
     */
    public static String os_ip;
    /**
     * 系统MAC地址
     */
    public static String os_mac;
    /**
     * 系统时间
     */
    public static Date os_date;
    /**
     * 系统CPU个数
     */
    public static Integer os_cpus;
    /**
     * 系统用户名
     */
    public static String os_user_name;
    /**
     * 用户的当前工作目录
     */
    public static String os_user_dir;
    /**
     * 用户的主目录
     */
    public static String os_user_home;

    /**
     * Java的运行环境版本
     */
    public static String java_version;
    /**
     * java默认的临时文件路径
     */
    public static String java_io_tmpdir;

    /**
     * java 平台
     */
    public static String sun_desktop;

    /**
     * 文件分隔符  在 unix 系统中是＂／＂
     */
    public static String file_separator;
    /**
     * 路径分隔符  在 unix 系统中是＂:＂
     */
    public static String path_separator;
    /**
     * 行分隔符   在 unix 系统中是＂/n＂
     */
    public static String line_separator;

    /**
     * 服务context
     **/
    public static String server_context;
    /**
     * 服务器名
     */
    public static String server_name;
    /**
     * 服务器端口
     */
    public static Integer server_port;
    /**
     * 服务器地址
     */
    public static String server_addr;
    /**
     * 获得客户端电脑的名字，若失败，则返回客户端电脑的ip地址
     */
    public static String server_host;
    /**
     * 服务协议
     */
    public static String server_protocol;

    public static SystemInfo SYSTEMINFO;

    public static SystemInfo getInstance() {
        return SYSTEMINFO = new SystemInfo();
    }


    public static SystemInfo getInstance(HttpServletRequest request) {
        return SYSTEMINFO = new SystemInfo(request);
    }

    public SystemInfo() {
        super();
        init();
    }

    public SystemInfo(HttpServletRequest request) {
        super();
        init();
        /**
         * 额外信息
         */
        ServerInfo(request);
    }

    /**
     * 输出信息
     */
    public void PrintInfo() {
        Properties props = System.getProperties();
        System.out.println("Java的运行环境版本：" + props.getProperty("java.version"));
        System.out.println("默认的临时文件路径：" + props.getProperty("java.io.tmpdir"));
        System.out.println("操作系统的名称：" + props.getProperty("os.name"));
        System.out.println("操作系统的构架：" + props.getProperty("os.arch"));
        System.out.println("操作系统的版本：" + props.getProperty("os.version"));
        System.out.println("文件分隔符：" + props.getProperty("file.separator"));   //在 unix 系统中是＂／＂
        System.out.println("路径分隔符：" + props.getProperty("path.separator"));   //在 unix 系统中是＂:＂
        System.out.println("行分隔符：" + props.getProperty("line.separator"));   //在 unix 系统中是＂/n＂
        System.out.println("用户的账户名称：" + props.getProperty("user.name"));
        System.out.println("用户的主目录：" + props.getProperty("user.home"));
        System.out.println("用户的当前工作目录：" + props.getProperty("user.dir"));
    }

    /**
     * 初始化基本属性
     */
    public void init() {
        Properties props = System.getProperties();

        java_version = props.getProperty("java.version");
        java_io_tmpdir = props.getProperty("java.io.tmpdir");
        os_name = props.getProperty("os.name");
        os_arch = props.getProperty("os.arch");
        os_version = props.getProperty("os.version");
        file_separator = props.getProperty("file.separator");
        path_separator = props.getProperty("path.separator");
        line_separator = props.getProperty("line.separator");

        os_user_name = props.getProperty("user.name");
        os_user_home = props.getProperty("user.home");
        os_user_dir = props.getProperty("user.dir");

        sun_desktop = props.getProperty("sun.desktop");

        os_date = new Date();
        os_cpus = Runtime.getRuntime().availableProcessors();

        try {
            ipMac();
        } catch (Exception e) {
            os_ip = "";
            os_mac = "";
        }
    }

    /*
     * 获取ip和mac地址
     */
    @SuppressWarnings("all")
    public void ipMac() throws Exception {
        InetAddress address = InetAddress.getLocalHost();
        NetworkInterface ni = NetworkInterface.getByInetAddress(address);
        ni.getInetAddresses().nextElement().getAddress();
        byte[] mac = ni.getHardwareAddress();
        String sIP = address.getHostAddress();
        String sMAC = "";
        Formatter formatter = new Formatter();
        for (int i = 0; i < mac.length; i++) {
            sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i],
                    (i < mac.length - 1) ? "-" : "").toString();

        }
        SystemInfo.os_ip = sIP;
        SystemInfo.os_mac = sMAC;
    }

    /*
     * 获取服务器信息、
     */
    public void ServerInfo(HttpServletRequest request) {
        server_name = request.getServerName();
        server_port = request.getServerPort();
        server_addr = request.getRemoteAddr();
        server_host = request.getRemoteHost();
        server_protocol = request.getProtocol();
        server_context = request.getContextPath();
    }
}